home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MotifMenuUI.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  162 lines

  1. /*
  2.  * @(#)MotifMenuUI.java    1.14 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.plaf.motif;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import com.sun.java.swing.*;
  25. import com.sun.java.swing.event.*;
  26. import com.sun.java.swing.plaf.*;
  27. import com.sun.java.swing.border.*;
  28. import com.sun.java.swing.plaf.basic.*;
  29. import java.io.Serializable;
  30.  
  31. import com.sun.java.swing.plaf.basic.BasicMenuUI;
  32. import com.sun.java.swing.plaf.basic.BasicGraphicsUtils;
  33.  
  34. /**
  35.  * A Motif L&F implementation of MenuUI.  
  36.  * <p>
  37.  * Warning: serialized objects of this class will not be compatible with
  38.  * future swing releases.  The current serialization support is appropriate
  39.  * for short term storage or RMI between Swing1.0 applications.  It will
  40.  * not be possible to load serialized Swing1.0 objects with future releases
  41.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  42.  * baseline for the serialized form of Swing objects.
  43.  *
  44.  * @version 1.14 02/02/98
  45.  * @author Georges Saab
  46.  * @author Rich Schiavi
  47.  */
  48. public class MotifMenuUI extends BasicMenuUI
  49. {
  50.  
  51.     protected ChangeListener changeListener;
  52.     protected static final int defaultTextIconGap = 2;
  53.  
  54.     public static ComponentUI createUI( JComponent x ) {
  55.     return new MotifMenuUI();
  56.     }
  57.     
  58.     protected void initListeners(JComponent c) {
  59.     super.initListeners(c);
  60.         changeListener = createChangeListener(c);
  61.     }
  62.     
  63.     protected void addListeners(JComponent c) {
  64.     super.addListeners(c);
  65.         ((JMenuItem)c).addChangeListener(changeListener);    
  66.     }
  67.  
  68.     protected void removeListeners(JComponent c) {
  69.     super.removeListeners(c);
  70.     ((JMenuItem)c).removeChangeListener(changeListener);
  71.     }
  72.  
  73.     protected ChangeListener createChangeListener(JComponent c) {
  74.     return new MenuChangeListener();
  75.     }
  76.     public void paint(Graphics g, JComponent c){
  77.     installDefaultIcons();
  78.     MotifGraphicsUtils.paintMenuItem(g,c,checkIcon,menuArrow,
  79.                      pressedBackground, pressedForeground,
  80.                      defaultTextIconGap);
  81.     }
  82.  
  83.  
  84.     boolean popupIsOpen(JMenu m,MenuElement me[]) {
  85.         int i;
  86.         JPopupMenu pm = m.getPopupMenu();
  87.  
  88.         for(i=me.length-1;i>=0;i--) {
  89.             if(me[i].getComponent() == pm)
  90.                 return true;
  91.         }
  92.         return false;
  93.     }
  94.     
  95.     public void processMouseEvent(JMenuItem item,MouseEvent e,MenuElement path[],MenuSelectionManager manager) {
  96.         Point p = e.getPoint();
  97.         if(e.getID() == MouseEvent.MOUSE_MOVED ||
  98.            e.getID() == MouseEvent.MOUSE_ENTERED ||
  99.            e.getID() == MouseEvent.MOUSE_EXITED)
  100.             return;
  101.     if (item.isEnabled() == false)
  102.       return;
  103.  
  104.         if(p.x >= 0 && p.x < item.getWidth() &&
  105.            p.y >= 0 && p.y < item.getHeight()) {
  106.         JMenu menu = (JMenu)item;
  107.             MenuElement selectedPath[] = manager.getSelectedPath();
  108.             if(!popupIsOpen(menu,selectedPath) || 
  109.            e.getID() == MouseEvent.MOUSE_RELEASED) {
  110.                 if(menu.isTopLevelMenu() || 
  111.            menu.getDelay() == 0) {
  112.                     MenuElement newPath[];
  113.                     MenuElement subElements[];
  114.                     if(e.getID() == MouseEvent.MOUSE_RELEASED) { /** Include first item **/
  115.                         newPath = new MenuElement[path.length+2];
  116.                         System.arraycopy(path,0,newPath,0,path.length);
  117.                         newPath[path.length] = menu.getPopupMenu();
  118.                         subElements = menu.getPopupMenu().getSubElements();
  119.                         if(subElements.length > 0) 
  120.                             newPath[path.length+1] = subElements[0];
  121.                         else {
  122.                             MenuElement tmp[] = new MenuElement[path.length+1];
  123.                             System.arraycopy(newPath,0,tmp,0,newPath.length-1);
  124.                             newPath = tmp;
  125.                         }
  126.                     } else { /** Do not include first item **/
  127.                         newPath = new MenuElement[path.length+1];
  128.                         System.arraycopy(path,0,newPath,0,path.length);
  129.                         newPath[path.length] = menu.getPopupMenu();
  130.                     }
  131.                     manager.setSelectedPath(newPath);
  132.                 } else {
  133.                     manager.setSelectedPath(path);
  134.                     setupPostTimer(menu);
  135.                 }
  136.             }
  137.         } else if(e.getID() == MouseEvent.MOUSE_RELEASED) {
  138.         Component c = manager.componentForPoint(e.getComponent(), e.getPoint());
  139.         if (c == null)
  140.         manager.clearSelectedPath();
  141.         }
  142.     }
  143.  
  144.     protected class MenuChangeListener implements ChangeListener, Serializable {
  145.  
  146.     public void stateChanged(ChangeEvent e) {
  147.         JMenuItem c = (JMenuItem)e.getSource();
  148.         if (c.isArmed() || c.isSelected()) {
  149.         c.setBorderPainted(true);
  150.         // c.repaint();
  151.         } else {
  152.         c.setBorderPainted(false);
  153.         }
  154.     }
  155.     }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.